package com.cht.commonUtil.infra;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cht.commonUtil.webInit.SP;
import com.cht.commonUtil.widgets.Widgets;

public class ManipulateGridEnv extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding( SP.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( SP.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		String oper = request.getParameter("oper");			//增刪改查那一種
		String subsysId = request.getParameter("subsysId");
		String envId = request.getParameter("id");
		String result = "";
		final String empId = request.getParameter("empId");
		
		try {
			conn = Widgets.getConn();
			
			/** 判別需進行何種操作 **/
			if ( "del".equalsIgnoreCase(oper) ) {
				sql = "DELETE FROM env WHERE envid=? AND subsysid=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, envId);
				ps.setString(2, subsysId);
				ps.execute();
				result = "刪除了"+envId;
			} else if ( "add".equalsIgnoreCase(oper) ) {
				if ( !isDuplicated(subsysId,envId,conn,ps,rs) ) {
					sql = "INSERT INTO env (subsysid,envname,creator,createdDateTime,envid) VALUES (?,?,?,?,?)";
					ps = conn.prepareStatement(sql);
					ps.setString(1, subsysId);
					ps.setString(2, request.getParameter("name") );
					ps.setString(3, empId);
					ps.setTimestamp(4, Widgets.cDT());
					ps.setString(5, envId);
					ps.execute();
					result = "新增完成 "+envId;
				} else {
					result = envId+" 已存在 !";		
				}
			} else if ( "edit".equalsIgnoreCase(oper) ) {
				sql = "UPDATE env SET envname=?,lastUpdator=?,lastUpdatedDateTime=? WHERE subsysid=? AND envid=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, request.getParameter("name") );
				ps.setString(2, empId);
				ps.setTimestamp(3, Widgets.cDT());
				ps.setString(4, subsysId);
				ps.setString(5, envId);
				ps.execute();
				result = "修改完成 "+envId;
			}			
		}  catch (Exception e) {
			result = "異動"+envId+"失敗!!\n\n";
			result += Widgets.errLog(e);
		} finally {
			try {
				if ( rs!= null) { rs.close(); }
				if ( ps!= null) { ps.close(); }
				if ( conn!= null) { conn.close(); }
			} catch (SQLException e) { e.printStackTrace(); }
		}
		
		response.getWriter().write(result);	
	}
	
	private boolean isDuplicated(String subsysId,String envId,Connection conn,PreparedStatement ps, ResultSet rs) throws SQLException {
		String sql = "SELECT COUNT(*) FROM env WHERE subsysid=? AND envid=?";
		
		ps = conn.prepareStatement(sql);
		ps.setString(1, subsysId);
		ps.setString(2, envId);
		rs = ps.executeQuery();
		rs.next();

		return rs.getInt(1) == 0?false:true;
	}

}
